{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modSGridGestion;

interface

uses
  Grids, BaseGrid, AdvGrid, SysUtils, ESBDates,
  Classes,
  sFonctionsChaines,
  modBaseDeDonnesRapport,
  modRapport,
  modGalaxiePlanetes,
  modStatsPlayers,
  modProfilsForAttack,
  modActionManager,
  modSondageList,
  modPlayerFinal,
  modApercuMsg,
  modFlotteEnVoyage,

  modListeAttaqueCollection,
  modListeAttaqueVaisseauxInfos,
  modBaseType, Graphics, modPlanete, modGIDtoSTR;



  // Page apercu (messages) (non utilisé)
  procedure sggUpdateMsgListApercu(Grille: TAdvStringGrid; MsgList: TCollectionAppMessage);
  

  // Informations sur un Element, prix, durée, etc
  procedure sggUpdateGidElements(Grille: TAdvStringGrid; GidElementsList: TList; currRessources, cProd: TOGRessourceAll; DoClear: boolean);

  // Liste de ses propres flottes de la page Flotte
  procedure sggUpdateFlotteList(Grille: TAdvStringGrid; FlotteList: TCollectionFlotteListEnVoyage; Player: TPlayerFinal);

  // Liste d'attaque
  procedure sggUpdateListeAttaque(Grille: TAdvStringGrid; Player: TPlayerFinal);

  // Champ de ruine
  procedure sggUpdateChampsDeRuine(Grille: TAdvStringGrid; BddCDR: TCollectionGalaxyTriee; BddRapport: TCollectionRapportHead; Player: TPlayerFinal);


implementation

uses modSGrid, gnugettext;


procedure sggUpdateChampsDeRuine(Grille: TAdvStringGrid; BddCDR: TCollectionGalaxyTriee; BddRapport: TCollectionRapportHead; Player: TPlayerFinal);
var DoClear: boolean;
    CurrInfos: PGalaxiePlanete;
    i, r: integer;
    peRapport: PRapportHead;
begin



    if (BddCDR = nil) or (BddCDR.Count = 0) then begin
       Grille.RemoveRows(2, Grille.RowCount - 2);

       for i := 0 to Grille.ColCount - 1 do begin
          Grille.Cells[i, 1] := '';
       end;

       if BddCDR = nil then
          Grille.Cells[3, 1] := _('Aucune liste chargée')
       else
          Grille.Cells[3, 1] := _('Aucun élément');


       exit;
    end;

    Grille.BeginUpdate;

    DoClear := true;

    if Grille.RowCount > 1 then begin
       if DoClear then begin
         // sgTMS.RemoveRows(2, sgTMS.RowCount - 2);

          // Supprime les lignes super flu
          if Grille.RowCount > BddCDR.Count then begin
             Grille.RemoveRows(2, Grille.RowCount - 1 - BddCDR.Count);
          end;

       end else
        //  j := 1;

    end else begin
     //  j := 0;
    end;

    r := 1;

    msgCurrDate := DateTimeToUnix(Now);

    for i := 0 to BddCDR.Count - 1 do begin
        CurrInfos := BddCDR.Items[i];


        if (Grille.RowCount = r) and (r > 1) then begin
           Grille.AddRow;
           r := Grille.RowCount - 1;
        end;

        peRapport := BddRapport.Get_Pointeur(CurrInfos.Location);
        (*if peRapport <> nil then
           FBaseCurrSystemSolaire.Add(peRapport);
           *)


        //UpdateDataChampDeRuine(r, CurrInfos, Grille, peRapport);
        msgUpdateChampDeRuineRow(Grille, r, CurrInfos, peRapport, Player);

        Inc(r);
    end;

    Grille.EndUpdate;

end;


procedure sggUpdateListeAttaque(Grille: TAdvStringGrid; Player: TPlayerFinal);
var DoClear: boolean;
    CurrInfos: PLAItem;
    i, r: integer;
begin

    if (Player = nil) or (Player.cListeDattaque = nil) then begin
       Grille.RemoveRows(2, Grille.RowCount - 2);
       Grille.Cells[0, 1] := _('Erreur');
       exit;
    end;

    if Player.cListeDattaque.Count = 0 then begin
       Grille.RemoveRows(2, Grille.RowCount - 2);
       Grille.Cells[0, 1] := _('Aucun élément');

       exit;
    end;


    Grille.BeginUpdate;

    DoClear := true;

    if Grille.RowCount > 1 then begin
       if DoClear then begin
          // Supprime les lignes super flu
          if Grille.RowCount > Player.cListeDattaque.Count then begin
             Grille.RemoveRows(2, Grille.RowCount - 1 - Player.cListeDattaque.Count);
          end;

       end;
    end;

    r := 1;


    msgCurrDate := DateTimeToUnix(Now);

    for i := 0 to Player.cListeDattaque.Count - 1 do begin
        CurrInfos := Player.cListeDattaque.Items[i];


        if (Grille.RowCount = r) and (r > 1) then begin
           Grille.AddRow;
           r := Grille.RowCount - 1;
        end;

        //UpdateDataListeAttaque(r, CurrInfos, Grille);
        msgUpdateDataListeAttaque(Grille, r, CurrInfos);

        Inc(r);
    end;

    Grille.EndUpdate;
end;


procedure sggUpdateGidElements(Grille: TAdvStringGrid; GidElementsList: TList; currRessources, cProd: TOGRessourceAll; DoClear: boolean);
var i, r: integer;
    CurrRes: POGItemCost;
    Header: TOGItemCost;
begin

    // AddItemInGridList
   // sgTMS.RemoveRows(1, sgTMS.RowCount - 2);

    if GidElementsList.Count = 0 then begin
       if Grille.RowCount > 1 then begin
          Grille.RemoveRows(2, Grille.RowCount - 2);
       end;

       for i := 0 to Grille.ColCount - 1 do begin
          Grille.Cells[i, 1] := '';
       end;

       Grille.Cells[1, 1] := _('Vide');
       
       exit;
    end;


    if Grille.RowCount > 1 then begin
       if DoClear then begin
         // sgTMS.RemoveRows(2, sgTMS.RowCount - 2);



          // Supprime les lignes super flu
          if Grille.RowCount > GidElementsList.Count then begin
             Grille.RemoveRows(2, Grille.RowCount - 1 - GidElementsList.Count);
          end;
       //   j := 0;

       end else
        //  j := 1;

    end else begin
     //  j := 0;
    end;


    r := 1;
    Grille.BeginUpdate;


    Header.Nom        := _('Ressources');
    Header.Prix       := currRessources;
    Header.Niveau     := -1;
    Header.CanBeBuildIn := 0;
    Header.CanBeBuildInOpt := 0;
    Header.Duree      := 0;
    Header.GIDType     := tgidAutre;
    if Grille.RowCount = r then begin
       Grille.AddRow;
       r := Grille.RowCount - 1;
    end;
    msgUpdateElementGIDRow(Grille, Header, currRessources, r);
    Inc(r);

    
    Header.Nom        := _('Production');
    Header.Prix       := cProd;
    Header.Niveau     := -1;
    Header.CanBeBuildIn := 0;
    Header.CanBeBuildInOpt := 0;
    Header.Duree      := 0;
    Header.GIDType     := tgidAutre;
    if Grille.RowCount = r then begin
       Grille.AddRow;
       r := Grille.RowCount - 1;
    end;
    msgUpdateElementGIDRow(Grille, Header, currRessources, r);
    Inc(r);
    
    for i := 0 to GidElementsList.Count - 1 do begin
        CurrRes := GidElementsList.Items[i];


        if Grille.RowCount = r then begin
           Grille.AddRow;
           r := Grille.RowCount - 1;
        end;

        msgUpdateElementGIDRow(Grille, CurrRes^, currRessources, r);

        Inc(r);
    end;

    Header.Nom        := _('Ressources');
    Header.Prix       := currRessources;
    Header.Niveau     := -1;
    Header.CanBeBuildIn := 0;
    Header.CanBeBuildInOpt := 0;
    Header.Duree      := 0;
    Header.GIDType     := tgidAutre;
    Grille.AddRow;
    r := Grille.RowCount - 1;
    msgUpdateElementGIDRow(Grille, Header, currRessources, r);

    Header.Nom        := _('Production');
    Header.Prix       := cProd;
    Header.Niveau     := -1;
    Header.CanBeBuildIn := 0;
    Header.CanBeBuildInOpt := 0;
    Header.Duree      := 0;
    Header.GIDType     := tgidAutre;
    Grille.AddRow;
    r := Grille.RowCount - 1;
    msgUpdateElementGIDRow(Grille, Header, currRessources, r);

    Grille.EndUpdate;

end;


procedure sggUpdateFlotteList(Grille: TAdvStringGrid; FlotteList: TCollectionFlotteListEnVoyage; Player: TPlayerFinal);
var DoClear: boolean;
    CurrInfos: PFlotteListEnVoyage;
    i, r: integer;
begin

    DoClear := true;
    if (FlotteList = nil) or (FlotteList.Count = 0) then begin
       Grille.RemoveRows(2, Grille.RowCount - 2);

       for i := 0 to Grille.ColCount - 1 do begin
          Grille.Cells[i, 1] := '';
       end;

       if FlotteList = nil then
          Grille.Cells[1, 1] := _('Aucune liste chargée')
       else
          Grille.Cells[1, 1] := _('Aucun message');

       exit;
    end;

    Grille.BeginUpdate;

    if Grille.RowCount > 1 then begin
       if DoClear then begin
         // sgTMS.RemoveRows(2, sgTMS.RowCount - 2);
         
          // Supprime les lignes super flu
          if Grille.RowCount > FlotteList.Count then begin
             Grille.RemoveRows(2, Grille.RowCount - 1 - FlotteList.Count);
          end;
       //   j := 0;

       end else
        //  j := 1;

    end else begin
     //  j := 0;
    end;

    r := 1;

    msgCurrDate := DateTimeToUnix(Now);

    for i := 0 to FlotteList.Count - 1 do begin
        CurrInfos := FlotteList.Items[i];


        if (Grille.RowCount = r) and (r > 1) then begin
           Grille.AddRow;
           r := Grille.RowCount - 1;
        end;

        //UpdateDataFlotteList(r, CurrInfos, Grille);
        msgUpdateFlotteListRow(Grille, r, CurrInfos, Player);

        Inc(r);
    end;

    Grille.EndUpdate;

end;




procedure sggUpdateMsgListApercu(Grille: TAdvStringGrid; MsgList: TCollectionAppMessage);
var DoClear: boolean;
    CurrInfos: PAppMessage;
    i, r: integer;
begin

    DoClear := true;
    if (MsgList = nil) or (MsgList.Count = 0) then begin
       Grille.RemoveRows(2, Grille.RowCount - 2);

       for i := 0 to Grille.ColCount - 1 do begin
          Grille.Cells[i, 1] := '';
       end;

       if MsgList = nil then
          Grille.Cells[3, 1] := _('Aucune liste chargée')
       else
          Grille.Cells[3, 1] := _('Aucun message');

       exit;
    end;

    Grille.BeginUpdate;

    if Grille.RowCount > 1 then begin
       if DoClear then begin
         // sgTMS.RemoveRows(2, sgTMS.RowCount - 2);



          // Supprime les lignes super flu
          if Grille.RowCount > MsgList.Count then begin
             Grille.RemoveRows(2, Grille.RowCount - 1 - MsgList.Count);
          end;
       //   j := 0;

       end else
        //  j := 1;

    end else begin
     //  j := 0;
    end;

    r := 1;

    msgCurrDate := DateTimeToUnix(Now);

    for i := 0 to MsgList.Count - 1 do begin
        CurrInfos := MsgList.Items[i];


        if (Grille.RowCount = r) and (r > 1) then begin
           Grille.AddRow;
           r := Grille.RowCount - 1;
        end;

        msgUpdateMsgListApercuRow(r, CurrInfos, Grille);

        Inc(r);
    end;

    Grille.EndUpdate;

end;


end.
